#!/usr/bin/env python

import gtk
import gettext
import os
from mxit import handles
from signals import editAccountWindowSignals

gettext.install('blendIT','locale');

class editAccountWindow(gtk.Window):

   def __init__(self,blend):
      gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL);
      self.set_title(_("Edit Account"));
      self.set_geometry_hints(min_width=100, min_height=100);
      #self.set_default_size(240,600); # TO DO - Persistant GUI Dimensions
      self.set_icon_from_file("images/blendit.png");
      self.blend = blend;
      
      self.sigs = editAccountWindowSignals.signals(blend);
      editArea = gtk.Table(rows=6,columns=2);
      self.createFields(editArea);

      cancelButton = gtk.Button(stock=gtk.STOCK_CANCEL);
      cancelButton.connect('clicked',self.sigs.on_cancel);

      saveButton = gtk.Button(stock=gtk.STOCK_OK);
      saveButton.connect('clicked',self.sigs.on_save);

      hButtonBox = gtk.HButtonBox();
      hButtonBox.add(cancelButton);
      hButtonBox.add(saveButton);
      hButtonBox.show_all();

      vbox = gtk.VBox(spacing=8);
      vbox.pack_start(editArea,True,True);
      vbox.pack_start(hButtonBox);
      vbox.show_all();
      self.add(vbox);
      self.set_border_width(4);


   def createFields(self,page):
      self.entries = {};

      # Content:
      self.entries['name'] = gtk.Entry();
      self.entries['name'].set_text(self.blend.currentAccount.name);
      self.entries['name'].show();
      page.attach(gtk.Label(_("Nickname: ")),0,1,0,1,gtk.FILL);
      page.attach(self.entries['name'],1,2,0,1,gtk.FILL);

      page.attach(gtk.Label(_("Private Number: ")),0,1,1,2,gtk.FILL);
      pnVbox = gtk.VBox();
      yes = gtk.RadioButton(None,_("Yes"));
      no = gtk.RadioButton(yes,_("No"));
      no.set_active(True);
      self.entries['private'] = (yes,no);
      if self.blend.currentAccount.private == handles.PrivateNumber.YES:
         yes.set_active(True);
      else:
         no.set_active(True);
      pnVbox.pack_start(yes);
      pnVbox.pack_start(no);
      alignP = gtk.Alignment(xalign=0);
      alignP.add(pnVbox);
      alignP.show();
      page.attach(alignP,1,2,1,2,gtk.FILL);

      page.attach(gtk.Label(_("Pin: ")),0,1,2,3,gtk.FILL);
      self.entries['pin'] = gtk.Entry();
      self.entries['pin'].set_visibility(False);
      self.entries['pin'].set_max_length(10);
      self.entries['pin'].show();
      self.entries['pin'].set_text(self.blend.currentAccount.password);
      self.entries['pin'].connect('changed',self.sigs.checkPin);
      alignThree = gtk.Alignment(xalign=0);      
      alignThree.add(self.entries['pin']);
      self.pinInfoLabel = gtk.Label(_('Pin must be more than 6 digits'));
      self.pinInfoLabel.show();
      pinVbox = gtk.HBox(spacing=3);
      pinVbox.pack_start(alignThree);
      pinVbox.pack_end(self.pinInfoLabel);
      pinVbox.show_all();
      page.attach(pinVbox,1,2,2,3,gtk.FILL);

      self.entries['rememberPin'] = gtk.CheckButton(_("Remember Pin"));
      self.entries['rememberPin'].show();
      if self.blend.currentAccount.rememberPassword == True:
         self.entries['rememberPin'].set_active(True);
      else:
         self.entries['rememberPin'].set_active(False);
      page.attach(self.entries['rememberPin'],1,2,3,4,gtk.FILL);

      # Date of Birth
      label = gtk.Label(_("Date of Birth: "));
      label.show();
      alignLabel = gtk.Alignment(xalign=0.5,yalign=0);
      alignLabel.add(label);
      alignLabel.show();
      page.attach(alignLabel,0,1,4,5,gtk.FILL);
      self.entries['dob'] = gtk.Calendar();
      self.entries['dob'].show();
      self.entries['dob'].select_month(self.blend.currentAccount.birthdate.tm_mon-1,
                                       self.blend.currentAccount.birthdate.tm_year);
      self.entries['dob'].select_day(self.blend.currentAccount.birthdate.tm_mday);
      self.entries['dob'].connect('day-selected',self.sigs.checkDate);
      self.entries['dob'].connect('next-year',self.sigs.checkDate);
      self.entries['dob'].connect('next-month',self.sigs.checkDate);
      self.calInfoLabel = gtk.Label();
      self.calInfoLabel.set_justify(gtk.JUSTIFY_CENTER);
      self.calInfoLabel.show();
      dobHbox = gtk.HBox();
      dobHbox.pack_start(self.entries['dob']);
      dobHbox.pack_start(self.calInfoLabel);
      alignSix = gtk.Alignment(xalign=0,yalign=0.5);
      alignSix.add(dobHbox);
      alignSix.show();
      page.attach(alignSix,1,2,4,5,gtk.FILL); 

      # Gender
      page.attach(gtk.Label(_("Gender: ")),0,1,5,6,gtk.FILL);
      male = gtk.RadioButton(None,_("Male"));
      male.show();
      female = gtk.RadioButton(male,_("Female"));
      female.show();
      if self.blend.currentAccount.gender == handles.Gender.FEMALE:
         female.set_active(True);
      else:
         male.set_active(True);

      self.entries['gender'] = (male,female);
      gendVbox = gtk.VBox();
      gendVbox.pack_start(self.entries['gender'][0]);
      gendVbox.pack_start(self.entries['gender'][1]);
      gendVbox.show();
      alignSev = gtk.Alignment(xalign=0);
      alignSev.add(gendVbox);
      alignSev.show();
      page.attach(alignSev,1,2,5,6,gtk.FILL);

      
      # Automatically Log in
      self.entries['autoLogin'] = gtk.CheckButton(_("Automatically Login"));
      if self.blend.currentAccount.autologin:
         self.entries['autoLogin'].set_active(True);
      else:
         self.entries['autoLogin'].set_active(False);
      self.entries['autoLogin'].show();
      page.attach(self.entries['autoLogin'],1,2,6,7,gtk.FILL);
